'Función que baja vida
'#INCLUDE once <clearBox.bas>
Sub damage()
    danyofrecacu=danyofrecacu+1
    if danyofrecacu>=experience THEN     
        danyofrecacu=0
        fxs(3,6)=fxs(3,6)+50     
        snd128(3)
        BajaMarc(danyo,1)     
        BORDER 2: PAUSE 1: BORDER 3: PAUSE 1: BORDER 0     
        pintado=0     
    End If
END Sub

'Mueve enemigo, lo pinta y realiza distintas comprobaciones
Sub mueveEnemigo(indice As UBYTE)
    Dim giroEX,giroEY As UBYTE
    Dim noChaser As UBYTE
    Dim pEX,pEY As BYTE     
    Dim classLocal As Byte
    Dim indLocal As Byte

    classLocal=MapaE(pantalla,indice,ECLASS)     
    indLocal=MapaE(pantalla,indice,0)
    noChaser=DatosE(indLocal,0)=77
    If giro(indice,0)=1 And noChaser  Then
        giro(indice,1)=giro(indice,1)-1
        If giro(indice,1)=0 Then
            giro(indice,0)=0: giro(indice,1)=WAITTURNING 
        End If
    Else
        pEX=MapaE(pantalla,indice,PEX)     
        pEY=MapaE(pantalla,indice,PEY)     
        putChars(pEX,pEY+2,2,2,@tiles(0,0)) 
        paint(pEX,pEY+2,2,2,7) 
        If noChaser Then    
            mueveEje(AVX,PEX,PXI,PXF,indice,giroEX)     
            mueveEje(AVY,PEY,PYI,PYF,indice,giroEY)     
            If giroEX<>0 And classLocal<4      
                MapaE(pantalla,indice,ECLASS)=classLocal+MapaE(pantalla,indice,AVX)
                classLocal=colMask(indice,0)
                colMask(indice,0)=colMask(indice,1)
                colMask(indice,1)=classLocal
            End If
        Else     
            chaser(pEX,pEY,giroEX,giroEY,10,indice,DatosE(indLocal,1),_
            DatosE(indLocal,2),DatosE(indLocal,3),DatosE(indLocal,4))     
        End If
        giro(indice,0)=(giroEX+giroEY) mod 2
    End If
    pEX=MapaE(pantalla,indice,PEX)     
    pEY=MapaE(pantalla,indice,PEY)     
    pausa()
    putChars(pEX,pEY+2,2,2,@enem((frameE1 mod 4)+4*MapaE(pantalla,indice,ECLASS),0))
    PaintData(pEX,pEY+2,2,2,@colMask(indice,0))
    If abs(y-pEY)<4-agachado  
        If abs(x-pEX)<2
            damage()
            pintado=0     
        End If
    End If        
END Sub

'Función que mueve una posición el enemigo
Sub mueveEje(AV As UBYTE,XY As UBYTE,I As UBYTE,F As UBYTE, indice As UBYTE, Byref giro As UBYTE)     
    Dim avLocal,indLocal As Byte
    avLocal=MapaE(pantalla,indice,AV)
    indLocal=MapaE(pantalla,indice,0)
    If avLocal<>0 Then     
        MapaE(pantalla,indice,XY)=MapaE(pantalla,indice,XY)+avLocal
        giro=0
        If MapaE(pantalla,indice,XY)=DatosE(indLocal,F) _     
            Or MapaE(pantalla,indice,XY)=DatosE(indLocal,I) Then 
            MapaE(pantalla,indice,AV)=avLocal*-1
            giro=1
        End If
    End If
END Sub

'Función del tipo de enemigo perseguidor
Sub chaser(EX As UBYTE, EY As UBYTE,Byref giroEX As UBYTE, Byref giroEY As UBYTE, dist As UBYTE, indice As UBYTE, x1 As UBYTE, y1 As UBYTE, x2 As UBYTE, y2 As UBYTE)
    If abs(x-EX)<dist Or abs(EX-x)<dist Then     
        MapaE(pantalla,indice,AVX)=0      
        If x<EX  
            If (x1<EX Or x2<EX) Then     
                MapaE(pantalla,indice,AVX)=-1     
            End If
        Else If x>EX 
            If (x1>EX Or x2>EX)    
                MapaE(pantalla,indice,AVX)=1      
            End If
        End If
        mueveEje(AVX,PEX,PXI,PXF,indice,giroEX)     
        MapaE(pantalla,indice,AVY)=0      
        If y<EY      
            If (y1<EY Or y2<EY) Then     
                MapaE(pantalla,indice,AVY)=-1 
            End If
        Else If y>EY     
            If (y1>EY Or y2>EY)
                MapaE(pantalla,indice,AVY)=1  
            End If
        End If
        mueveEje(AVY,PEY,PYI,PYF,indice,giroEY)     
    End If
END Sub


